#!/bin/bash

#需要根据使用者修改的参数
root_path='/Users/wsh/.package_git_workspace'
#产出文件夹
build_root_path="${root_path}/build"

#需要根据输入参数，代码自动修改的自动
git_path="${root_path}/loan-ios"
project_path="${root_path}/loan-ios/Loan"
git_source='ssh://git@stash.weimob.com:7999/loan/loan-ios.git'
scheme='Loan'

#可配置的参数
app='loan'
branch='dev'
configuration='release'

printHelp(){
	echo 'Usage: ant [-a {app-name}] [-b {branch}] [-c {configuration}]'
	echo 'eg: '
	echo '    ant'
	echo '    ant -a loan'
	echo '    ant -b 1.2.1'
	echo '    ant -a loan -b 1.2.1 -c release'
	echo ''
	printf "%4s %-8s %s\n" ' ' '-h' 'optional, help'
	printf "%4s %-8s %s\n" ' ' '-a' "optional, app, eg.\"loan\",\"king\", default \"${app}\""
	printf "%4s %-8s %s\n" ' ' '-b' "optional, branch, eg.\"dev\",\"1.2.1\",etc, default \"${appIdentifier}\""
	printf "%4s %-8s %s\n" ' ' '-c' "configuration, server, eg.\"dev\", \"pl\", \"release\", default \"${config}\""
	echo ''
	echo Created by 萌店金融 on 10/19/17.
	echo Copyright © 2017 WeiMob. All rights reserved.
}

handleParameterError(){
	#sourceType只能为hotfix、hybrid
	if [[ ${app} != "loan" && ${app} != "king" ]]; then
		echo "error paramerter -a: ${app}"
		exit
	fi
	#config只能为dev,pl,release
	if [[ ${configuration} != "dev" && ${configuration} != "pl" &&  ${configuration} != "release" ]]; then
		echo "error paramerter -c: ${configuration}"
		exit
	fi
}

isContainsHelpCommand=false
isParameterError=false

while getopts "a:b:c:h" arg #选项后面的冒号表示该选项需要参数
do
        case $arg in
        	a)
            	app=${OPTARG}
                ;;
            b)
            	branch=${OPTARG}
                ;;
            c)
                configuration=${OPTARG}
                ;;
			h)
				isContainsHelpCommand=true
				;;
            ?)  #当有不认识的选项的时候arg为?
        # exit 1

        esac
done

if [ ${isContainsHelpCommand} = true ] ; then
	printHelp
	exit
fi

#检查参数
handleParameterError

if [[ ${app} == "king" ]]; then
	git_path="${root_path}/loan-king-ios"
	project_path="${root_path}/loan-king-ios/LoanKing"
	git_source='ssh://git@stash.weimob.com:7999/loan/loan-king-ios.git'
	scheme='LoanKing'
fi

if [[ ${configuration} == "dev" ]]; then
	configuration='HocDev'
elif [[ ${configuration} == "pl" ]]; then
	configuration='HocPL'
else
	configuration='HocRelease'
fi

#检查是否创建文件夹
if [[ ! -d  ${root_path} ]]; then
	mkdir -p ${root_path}
fi

#clone工程
if [[ ! -d  "${git_path}/.git" ]]; then
	cd ${root_path}; git clone ${git_source}
fi

cd ${git_path}
git reset --hard
git clean -df
result=`git checkout ${branch}`

# Your branch is up-to-date with 'origin/1.2.1'
# Branch 1.2.1 set up to track remote branch 1.2.1 from origin.
if [[ ${result} != *'Your branch is up-to-date with'*  && ${result} != *'set up to track remote branch'* && ${result} != *'Your branch is behind'* ]]; then
	echo "git checkout failed"
	exit
fi

result=`git pull`

echo ${result} 

if [[ ${result} == *'fail'* ]]; then
	echo 'git pull failed'
	exit
fi

#开始打包
current_time=`date "+%Y-%m-%d %H-%M-%S"`
build_path="${build_root_path}/${current_time}"
mkdir -p "${build_path}"
archive_path="${build_path}/${configuration}-Loan.xcarchive"

echo "build package..."

cd ${project_path}
#clean
xcodebuild clean -workspace ${scheme}.xcworkspace -scheme ${scheme} -configuration ${configuration}
#archive
xcodebuild archive -workspace ${scheme}.xcworkspace -scheme ${scheme} -configuration ${configuration} \
CODE_SIGN_IDENTITY='iPhone Distribution: Shanghai Huishuo Information Technology Co., Ltd.' \
-archivePath "${archive_path}"
#exportArchiv
xcodebuild -exportArchive -archivePath "${archive_path}" -exportPath "${build_path}" -exportOptionsPlist "${root_path}/ExportOptions.plist"

fir publish "${build_path}/Loan.ipa" -T cc7ee271809db6e825b7f93f6745993b

#萌店包管理
curl --form "uploadFile=@${build_path}/Loan.ipa" https://appmgr.vd.cn/MainService/fileUpload
